<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            placeholder="请输入账号"
            clearable
            @clear="getAdminList()"
            v-model="queryPageInfo.name"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getAdminList()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-input
            placeholder="请输入电话"
            clearable
            @clear="getAdminList()"
            v-model.number="queryPageInfo.phone"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getAdminList()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addUser">添加用户</el-button>
        </el-col>
      </el-row>

      <el-table :data="adminList" border stripe>
        <el-table-column type="index" label=""></el-table-column>
        <el-table-column prop="userName" label="账号"></el-table-column>
        <el-table-column prop="userNickname" label="昵称"></el-table-column>
        <el-table-column prop="userPhone" label="电话"></el-table-column>
        <el-table-column prop="userState" label="状态">
          <template slot-scope="scope">
            <el-switch
              @change="editUserState(scope.row.id)"
              v-model="scope.row.userState"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="userState" label="管理权限">
          <template slot-scope="scope">
            <el-switch
              @change="editUserLevel(scope.row.id)"
              v-model="scope.row.userLevel"
              :active-value="2"
              :inactive-value="1"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              content="修改"
              placement="top"
              :enterable="false"
            >
              <el-button
                @click="editUser(scope.row.id)"
                type="primary"
                icon="el-icon-edit"
                size="mini"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              effect="dark"
              content="删除"
              placement="top"
              :enterable="false"
            >
              <el-button
                @click="deletebyid(scope.row.id)"
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryPageInfo.pageNum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryPageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog
        title="修改用户"
        :visible.sync="editDialogVisible"
        @close="editDialogclose"
        width="50%"
      >
        <el-form
          :model="editForm"
          :rules="editFormRules"
          ref="editFormRef"
          label-width="80px"
        >
          <el-form-item label="账号" prop="userName">
            <el-input v-model="editForm.userName"></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="userNickname">
            <el-input v-model="editForm.userNickname"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="userPhone">
            <el-input v-model="editForm.userPhone"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="userPassword">
            <el-input v-model="editForm.userPassword"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editDialogSubmit">确 定</el-button>
        </span>
      </el-dialog>

      <el-dialog
        title="添加用户"
        :visible.sync="addDialogVisible"
        @close="addDialogclose"
        width="50%"
      >
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="70px"
        >
          <el-form-item label="账号:" prop="userName">
            <el-input v-model="addForm.userName"></el-input>
          </el-form-item>

          <el-form-item label="昵称:" prop="userNickname">
            <el-input v-model="addForm.userNickname"></el-input>
          </el-form-item>

          <el-form-item label="密码:" prop="userPassword">
            <el-input v-model="addForm.userPassword"></el-input>
          </el-form-item>

          <el-form-item label="手机:" prop="userPhone">
            <el-input v-model="addForm.userPhone"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addDialogSubmit">确 定</el-button>
        </span>
      </el-dialog>

  </div>
</template>

<script>
import { getUserData, updateUserState, getUserById, updateUser, deleteUserById, addUser, updateUserLevel } from '@/api/user'
export default {
  name: 'UserList',
  data () {
    return {
      queryPageInfo: {
        name: '',
        phone: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      adminList: [],
      editDialogVisible: false,
      editForm: {},
      editFormRules: {
        userName: [
          { required: true, message: '请输入用户账号', trigger: 'blur' },
          {
            min: 8,
            max: 16,
            message: '长度在 8 到 16 个字符',
            trigger: 'blur'
          }
        ],
        userNickname: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '长度在 2 到 10 个字符',
            trigger: 'blur'
          }
        ],
        userPhone: [
          { required: true, trigger: 'blur', message: '请输入11位手机号' },
          { required: true, trigger: 'blur', min: 11, max: 11, message: '长度不符合' }
        ],
        userPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 16,
            message: '长度在 6 到 16 个字符',
            trigger: 'blur'
          }
        ]
      },
      addDialogVisible: false,
      addForm: {
        userName: '',
        userNickname: '',
        userPhone: '',
        userPassword: ''
      },
      addFormRules: {
        userName: [
          { required: true, message: '请输入用户账号', trigger: 'blur' },
          {
            min: 8,
            max: 16,
            message: '长度在 8 到 16 个字符',
            trigger: 'blur'
          }
        ],
        userNickname: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '长度在 2 到 10 个字符',
            trigger: 'blur'
          }
        ],
        userPhone: [
          { required: true, trigger: 'blur', message: '请输入11位手机号' },
          { required: true, trigger: 'blur', min: 11, max: 11, message: '长度不符合' }
        ],
        userPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 16,
            message: '长度在 6 到 16 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async getAdminList () {
      const res = await getUserData(this.queryPageInfo)
      this.adminList = res.data.list
      this.total = res.data.total
      // console.log(res)
    },
    handleSizeChange (newPageSize) {
      this.queryPageInfo.pageSize = newPageSize
      this.getAdminList()
    },
    handleCurrentChange (newPageNum) {
      this.queryPageInfo.pageNum = newPageNum
      this.getAdminList()
    },
    async editUserState (id) {
      const res = await updateUserState(id)
      this.$message.success(res.data)
    },
    async editUser (id) {
      this.editDialogVisible = true
      const res = await getUserById(id)
      this.editForm = res.data
    },
    async deletebyid (id) {
      const res = await deleteUserById(id)
      this.getAdminList()
      this.$message.success(res.data)
    },
    editDialogclose () {
      // this.editDialogVisible = false
      this.$refs.editFormRef.resetFields()
    },
    editDialogSubmit () {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return false
        const res = await updateUser(this.editForm)
        // console.log(res)
        this.editDialogVisible = false
        this.getAdminList()
        this.$message.success(res.data)
      })
    },
    addDialogclose () {
      this.$refs.addFormRef.resetFields()
    },
    addUser () {
      this.addDialogVisible = true
    },
    addDialogSubmit () {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return false
        const res = await addUser(this.addForm)
        // console.log(res)
        this.addDialogVisible = false
        this.getAdminList()
        this.$message.success(res.data)
      })
    },
    async editUserLevel (id) {
      const res = await updateUserLevel(id)
      this.$message.success(res.data)
    }
  },
  created () {
    this.getAdminList()
  }
}
</script>

<style>

</style>
